package Controller;

import Service.ResourceCategoryService;
import domain.ResourceCategory;
import domain.ResponseResult;
import org.apache.ibatis.mapping.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ResourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /*
            资源分类表的查询
     */
    @RequestMapping("/findAllResourceCategory")
    public ResponseResult findAllResourceCategory(){

        List<ResourceCategory> resourceCategory = resourceCategoryService.findAllResourceCategory();

        return new ResponseResult(true,200,"资源列表查询成功",resourceCategory);
    }

    /*
        添加&修改资源分类表
    */
    @RequestMapping("/saveOrUpdateResource")
    public ResponseResult saveOrUpdateResource(@RequestBody ResourceCategory resourceCategory){

        //判断是添加操作 or 修改操作
        //新增操作
        if(resourceCategory.getId() == null){
            resourceCategoryService.saveResourceCategory(resourceCategory);
            return new ResponseResult(true,200,"资源列表添加信息成功",resourceCategory);
        }else{
            // 修改操作
            resourceCategoryService.updateResourceCategory(resourceCategory);
            return new ResponseResult(true,200,"资源列表更新成功",resourceCategory);
        }
    }

    /*
            删除资源分类表信息
     */
    @RequestMapping("/deleteResource")
    public ResponseResult deleteResource(int id){

        resourceCategoryService.deleteResourceCategory(id);
        return new ResponseResult(true,200,"删除资源列表分类成功",null);
    }
}
